package com.freeter.modules.controller;


import com.freeter.modules.gjs.entity.TransferItems;
import com.freeter.modules.gjs.service.TransferItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/transfer-items")
@CrossOrigin(origins = "*")
public class TransferItemsController {

    @Autowired
    private TransferItemsService transferItemsService;

    @GetMapping
    public ResponseEntity<List<TransferItems>> getAllTransferItems() {
        List<TransferItems> transferItems = transferItemsService.getAllTransferItems();
        return ResponseEntity.ok(transferItems);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TransferItems> getTransferItemsById(@PathVariable Long id) {
        TransferItems transferItems = transferItemsService.getTransferItemsById(id);
        if (transferItems != null) {
            return ResponseEntity.ok(transferItems);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/transfer-no/{transferNo}")
    public ResponseEntity<List<TransferItems>> getTransferItemsByTransferNo(@PathVariable String transferNo) {
        List<TransferItems> transferItems = transferItemsService.getTransferItemsByTransferNo(transferNo);
        return ResponseEntity.ok(transferItems);
    }

    @GetMapping("/item/{itemCode}")
    public ResponseEntity<List<TransferItems>> getTransferItemsByItemCode(@PathVariable String itemCode) {
        List<TransferItems> transferItems = transferItemsService.getTransferItemsByItemCode(itemCode);
        return ResponseEntity.ok(transferItems);
    }

    @GetMapping("/from-site/{fromSiteCode}")
    public ResponseEntity<List<TransferItems>> getTransferItemsByFromSiteCode(@PathVariable String fromSiteCode) {
        List<TransferItems> transferItems = transferItemsService.getTransferItemsByFromSiteCode(fromSiteCode);
        return ResponseEntity.ok(transferItems);
    }

    @GetMapping("/to-site/{toSiteCode}")
    public ResponseEntity<List<TransferItems>> getTransferItemsByToSiteCode(@PathVariable String toSiteCode) {
        List<TransferItems> transferItems = transferItemsService.getTransferItemsByToSiteCode(toSiteCode);
        return ResponseEntity.ok(transferItems);
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<TransferItems>> getTransferItemsByStatus(@PathVariable String status) {
        List<TransferItems> transferItems = transferItemsService.getTransferItemsByStatus(status);
        return ResponseEntity.ok(transferItems);
    }

    @PostMapping
    public ResponseEntity<TransferItems> createTransferItems(@RequestBody TransferItems transferItems) {
        TransferItems createdTransferItems = transferItemsService.createTransferItems(transferItems);
        return ResponseEntity.ok(createdTransferItems);
    }

    @PutMapping("/{id}")
    public ResponseEntity<TransferItems> updateTransferItems(@PathVariable Long id, @RequestBody TransferItems transferItems) {
        transferItems.setId(id);
        TransferItems updatedTransferItems = transferItemsService.updateTransferItems(transferItems);
        return ResponseEntity.ok(updatedTransferItems);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTransferItems(@PathVariable Long id) {
        boolean deleted = transferItemsService.deleteTransferItems(id);
        if (deleted) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/transfer-no/{transferNo}")
    public ResponseEntity<Void> deleteTransferItemsByTransferNo(@PathVariable String transferNo) {
        boolean deleted = transferItemsService.deleteTransferItemsByTransferNo(transferNo);
        if (deleted) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }
}